package com.hack.qiaoqiao.serve;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class FriendsContactsDataBase extends SQLiteOpenHelper{
	private static final int DATABASE_VERSION = 1 ;
	private static final String DATABASE_NAME = "CONTACT_FRIENDS";
	private static final String CREATE_CONTACTS_TABLE = "CREATE TABLE contact_info (_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
	"people_name TEXT , phone_number TEXT , indication TEXT )";
	private static final String TABLE_NAME = "contact_info";
	private SQLiteDatabase Database;
	public FriendsContactsDataBase(Context context, String name,
			CursorFactory factory, int version) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		this.Database = db;
		db.execSQL(CREATE_CONTACTS_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}
	
	public void saveData(SQLiteDatabase db , String people_name,String people_number,String indication){
		ContentValues cv = new ContentValues();
		cv.put("people_name", people_name);
		cv.put("people_number", people_number);
		cv.put("indication", indication);
		this.insert(cv);
	}

	private void insert(ContentValues cv) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = this.getWritableDatabase();
		db.insertOrThrow(TABLE_NAME, null, cv);
		db.close();
	}
	
	public  Cursor query (String id){
		SQLiteDatabase db = this.getWritableDatabase(); 
		Cursor c = db.query(TABLE_NAME, null,"_id="+id, null, null, null, null);
		if (c == null){
			return null;
		}
		return c;
	}
	
	public ArrayList<String[]> GetFriends(){
		ArrayList<String[]> Persons = new ArrayList<String[]>() ;
		SQLiteDatabase db =  this.getWritableDatabase();
		Cursor c = db.query(TABLE_NAME, null, null, null, null, null, null, null);
		c.moveToFirst();
		while(c.moveToNext()){
			String name = (c.getString(c.getColumnIndex("people_name")));
			String number = c.getString(c.getColumnIndexOrThrow("phone_number")); 
			String indication = c.getString(c.getColumnIndex("indication"));
			
			String [] person = {name ,number, indication};
			Persons.add(person);
		}
		if (Persons == null ){
			return null; 
		}
		return Persons ;
		
		
	}
	
	public void  closeDatabase(){
		if (this.Database != null){
			this.Database.close();
		}
	}
	public void DelData (String id){
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_NAME, "_id=?", new String[]{String.valueOf(id)});
	}
	public boolean isEmpty(){
		SQLiteDatabase db=this.getReadableDatabase();
		Cursor c=db.query(TABLE_NAME, null, null, null, null, null, null);
		if(c==null)
			return true;
		if(!c.moveToFirst())
			return true;
		if(c.getCount()>0)
			return false;
		else
			return true;
	}
	

}
